<template>
  <div>
    <ylheader @save="savecaogao" @send='sendcomment' :lefttext="true" :lefttexts="'取消'" :title="'发表说说'" :righttext="true" :righttexts="'发送'"></ylheader>
    <div>
      <div class="loadmore-container" ref="searchWrapper">
      <div class="loadmore-ul">
        <publishshuoshuo :textareatexts="content" :thumbnailslist="pics"  @haha="textareatext" @commentpics="uploadpics" @changeTextArea="changeTextArea"></publishshuoshuo>
      </div>
      </div>
    </div>
  </div>
</template>
<script>
import ylheader from "src/components/yl-header.vue";
import publishshuoshuo from "src/components/addcomments.vue";
import api from "src/api/index";
import { mapState, mapMutations } from "vuex";
export default {
  name:"publishbigsay",
  data() {
    return {
      content: "",
      pics: [],
      uploadpictures: [],
      picstring: [],
      caogaoId: "",
      doubleclick: false
    };
  },
  mounted() {
    this.lookbigsay().then(data => {
      return this.initScroll();
    });
  },
  computed: {
    ...mapState(["saystatus"])
  },

  components: {
    ylheader,
    publishshuoshuo
  },
  methods: {
    ...mapMutations(["changesaystatus"]),
    /**
       * 查询评论草稿
       */
    lookbigsay() {
      let p = new Promise((resolve, reject) => {
        api.Querylookbigsay({}).then(res => {
          if (res.success) {
            if (res.data) {
              this.pics = res.data.picture;
              this.content = res.data.content;
              this.caogaoId = res.data.id;
            }
          }
          resolve("获取评论草稿");
        });
      });
      return p;
    },
    //初始化scroll
    initScroll() {
      let p = new Promise((resolve, reject) => {
        this.$nextTick(() => {
          let searchWrapper = this.$refs.searchWrapper;
          this.setScroll(searchWrapper, this);
        });
        resolve("初始化scroll结束...");
      });
      return p;
    },
    setScroll(searchWrapper, _this) {
      localStorage.tempPageHeight = document.getElementsByClassName(
        "loadmore-ul"
      )[0].scrollHeight;
      _this.myscroll = new iScroll(searchWrapper, {
        //上拉时触发
        onScrollMove: function() {
          if (
            this.y <= this.wrapperH - this.scroller.clientHeight - 50 &&
            _this.is_r == false
          ) {
            //加载数据
            _this.is_r = true;
          }
        },
        onScrollEnd: function() {},
        onScrollStart: function() {
          if (
            localStorage.tempPageHeight !=
            document.getElementsByClassName("loadmore-ul")[0].scrollHeight
          ) {
            this.refresh();
            localStorage.tempPageHeight = document.getElementsByClassName(
              "loadmore-ul"
            )[0].scrollHeight;
          }
        }
      });
    },
    textareatext(data) {
      this.content = data;
    },
    uploadpics(data) {
      this.uploadpictures = data;
    },
    savecaogao() {
      if (this.doubleclick) {
        return;
      }
       this.doubleclick = true;
      this.uploadpictures = [...this.pics];

      const array = [];
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src);
      });
      this.picstring = array.join(",");

      if (!this.content && !this.picstring) {
         this.doubleclick=false;
        this.$router.go(-1);
        return;
      }

      const says = {
        id: this.caogaoId,
        content: this.content,
        draft: 1,//草稿
        address: localStorage.getItem("park")
      };
      const comment = {
        says: says,
        pictureJson: this.picstring,
        token: localStorage.getItem("token")
      };
      api.Querybublishbigsay(comment).then(res => {
        if (res.success) {
          this.$router.go(-1);
        } else {
           this.doubleclick=false;
          this.$toast(res.msg);
        }
      }).catch(error=>{
         this.doubleclick=false;
      });
;
    },
    sendcomment() {
      this.changesaystatus("1");
      this.uploadpictures = [...this.pics];
      const array = [];
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src);
      });
      this.picstring = array.join(",");

      if (this.doubleclick) {
        return;
      }
       this.doubleclick = true;
      if (!this.content && !this.picstring) {
        this.$toast("请编写说说");
        this.doubleclick=false;
        return;
      }
      // this.content = this.$util.getTextUrl(this.content);//识别链接
      const says = {
        content: this.content,
        draft: 0,
        address: localStorage.getItem("park"),
        id: this.caogaoId
      };
      const comment = {
        says: says,
        pictureJson: this.picstring,
        token: localStorage.getItem("token")
      };
      api.Querybublishbigsay(comment).then(res => {
        this.doubleclick = false;
        if (res.success) {
          this.$util.saveFootPrint({actType:3,typeValue:res.value});//用户轨迹
          if(this.$route.query.tag==0){
            this.$router.go(-1);
          }else if(this.$route.query.tag==1){
            this.$router.push({path:"/shuoshuo"});
          }
        } else {
          this.$toast(res.msg);
        }
      });
    },
    changeTextArea() {
      this.$nextTick(() => {
        this.myscroll.refresh();
      });
    }
  }
};
</script>
<style lang="less" scoped>
.loadmore-container {
  height: 95vh;
  overflow: hidden;
  margin: 0 auto;
}
.loadmore-ul {
  padding-bottom: 0.2rem;
  list-style: none;
}
</style>
